package com.chenhao.springcloud.chenhaoribbonservice;

import com.chenhao.springcloud.chenhaoribbonservice.config.RibbonConfig;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableDiscoveryClient
@SpringBootApplication
@RibbonClient(name="user-service", configuration= RibbonConfig.class)
public class ChenhaoRibbonServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChenhaoRibbonServiceApplication.class, args);
    }

    @Configuration
    @ConditionalOnClass({MeterRegistry.class})
    static class MeterRegistryConfig {
        @Value("${spring.application.name}")
        private final String applicationName = "APP_NAME";

        @Bean
        MeterRegistryCustomizer <MeterRegistry> appMetricsCommonTags() {
            return registry -> registry.config().commonTags("application", applicationName);
        }
    }

}
